/*
********************************** LICENSE *************************************
 * Copyright (c) 2012 cablop.net
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 *  IN THE SOFTWARE.
********************************** LICENSE *************************************
 *
 * Dependencies:
 *     jQuery: http://jquery.com/
 *     jquery-numberformatter: http://code.google.com/p/jquery-numberformatter/
 */

$.__cablop_jQuery_numberFormatterExtensions__ = {};
$.__cablop_jQuery_numberFormatterExtensions__.defaults = {};
$.__cablop_jQuery_numberFormatterExtensions__.defaults.numericFormatProperties = {
	format: '#,##0.00',
	locale: 'us'
};

$.fn.__cablop_jQuery_numberFormatterExtensions__unformatMoneyInput = function (numericFormatProperties) {
	if (typeof numericFormatProperties == 'undefined') {
		numericFormatProperties = $.__cablop_jQuery_numberFormatterExtensions__.defaults.numericFormatProperties;
	};
	return this.each(function() {
		if ($(this).is(":input")) {
			//if ($(this).val() === '') {
			//	$(this).val(0);
			//}//			it seems that paseNumber() already achieves this effect
			if (!($.isNumeric($(this).val()))) {
				$(this).parseNumber(numericFormatProperties);
			}
			return $(this).val();
		}
	});
};

$.fn.__cablop_jQuery_numberFormatterExtensions__formatMoneyInput = function (numericFormatProperties) {
	if (typeof numericFormatProperties == 'undefined') {
		numericFormatProperties = $.__cablop_jQuery_numberFormatterExtensions__.defaults.numericFormatProperties;
	};
	return this.each(function() {
		if ($(this).is(":input")) {
			if ($(this).val() === '') {
				$(this).val(0);
			}
			var ret;
			if (($.isNumeric($(this).val()))) {
				ret = $(this).val();
				$(this).formatNumber(numericFormatProperties);
			}
			else {
				ret = $(this).parseNumber(numericFormatProperties, false);
			}
			return ret;
		}
	});
};

$.fn.__cablop_jQuery_numberFormatterExtensions__setMoneyInputValue = function (value, numericFormatProperties) {
	if (typeof numericFormatProperties == 'undefined') {
		numericFormatProperties = $.__cablop_jQuery_numberFormatterExtensions__.defaults.numericFormatProperties;
	};
	return this.each(function() {
		if ($(this).is(":input")) {
			$(this).val(
				$.isNumeric(value) ?
					value :
					Number.NaN
			);
			$(this).formatNumber(numericFormatProperties);
		}
	});
};

$.fn.__cablop_jQuery_numberFormatterExtensions__getMoneyInputValue = function (numericFormatProperties) {
	if (typeof numericFormatProperties == 'undefined') {
		numericFormatProperties = $.__cablop_jQuery_numberFormatterExtensions__.defaults.numericFormatProperties;
	};
	if ($(this).is(":input")) {
		var value =
			$.isNumeric($(this).val()) ?
				parseFloat($(this).val(), 10) :
				$(this).parseNumber(numericFormatProperties, false)*1;
		return value;
	}
};
